﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Linq;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using DataAccessObject;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;

namespace ThangNM.Admin
{
    public partial class PermissionUI : System.Web.UI.Page
    {
        public AccountEO objAccountEO_Temp
        {
            get { return (AccountEO)ViewState["objAccountEO_Temp"]; }
            set { ViewState["objAccountEO_Temp"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Title_FormUC1.setTitle(ConstEx.Table_Name_C.Permission, ConstEx.funcButtonToolBar.NA);
                MsgUC1.setClearMsg();
                //if (ShareDAO.UserDAO.SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                //{
                //    pnlMain.Visible = false;
                //    Commons.setAccessDenied(Page, MsgUC1);
                //    return;
                //}
                if (!IsPostBack)
                {
                    ToolBarUC1.ViewGroupButtonByFunction(ConstEx.funcButtonToolBar.DETAIL);
                    ToolBarUC1.ViewButtonByFunction(ConstEx.FunctionType.Update);
                    BindData(true);
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void EventButtonToolBar_Click(object sender, CustomerEventArgs e)
        {
            //if (ShareDAO.UserDAO.SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), e.iType) == false)
            //{
            //    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.DANGER, MessagesExEx.Khong_Duoc_Su_Dung_Chuc_Nang_Nay);
            //    return;
            //}
            switch (e.iType)
            {
                case ConstEx.funcButtonToolBar.INSERT_LIST: break;
                case ConstEx.funcButtonToolBar.INSERT_ITEM: break;
                case ConstEx.funcButtonToolBar.UPDATE_ITEM:
                    SaveChange();
                    break;
                case ConstEx.funcButtonToolBar.DELETE_LIST: break;
                case ConstEx.funcButtonToolBar.DELETE_ITEM: break;
                case ConstEx.funcButtonToolBar.REFRESH_LIST: break;
                case ConstEx.funcButtonToolBar.EXPORT_EXCEL: break;
                case ConstEx.funcButtonToolBar.RESET_FORM: break;
            }
        }

        protected void SaveChange()
        {
            try
            {
                if (chkSetDefault.Checked == false)
                {
                    if ((new FunctionDAO()).Update_sGroup(Convert.ToInt16(ddliPermission.SelectedValue), WebEx.getSelect_ListCheckBox(cblPK_iFunctionID, true)))
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, String.Format(MessagesEx.Sua_Thanh_Cong_Ma_Ten, ConstEx.Table_Name_C.Account, ddlsUsername.SelectedValue, ddlsUsername.SelectedItem.Text));
                    else
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, String.Format(MessagesEx.Sua_That_Bai_Ma_Ten, ConstEx.Table_Name_C.Account, ddlsUsername.SelectedValue, ddlsUsername.SelectedItem.Text));
                }
                else
                {
                    AccountEO _AccountEO = new AccountEO();
                    _AccountEO.PK_iAccountID = Convert.ToInt32(ddlsUsername.SelectedValue);
                    _AccountEO.iPermission = Convert.ToInt16(ddliPermission.SelectedValue);
                    _AccountEO.sPermission = WebEx.getSelect_ListCheckBox(cblPK_iFunctionID, true);
                    _AccountEO.sPassword = Security.EncryptString(_AccountEO.sPermission, ddlsUsername.SelectedValue);
                    if ((new ShareDAO.UserDAO()).Update_Permission(_AccountEO))
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, String.Format(MessagesEx.Sua_Thanh_Cong_Ma_Ten, ConstEx.Table_Name_C.Account, ddlsUsername.SelectedValue, ddlsUsername.SelectedItem.Text));
                    else
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, String.Format(MessagesEx.Sua_That_Bai_Ma_Ten, ConstEx.Table_Name_C.Account, ddlsUsername.SelectedValue, ddlsUsername.SelectedItem.Text));
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData(Boolean isPostBack)
        {
            if (isPostBack)
                BindData_DDL();
            AccountEO _AccountEO = (new ShareDAO.UserDAO()).SelectItem(Convert.ToInt32(ddlsUsername.SelectedValue), null);
            ddliPermission.SelectedValue = Convert.ToString(_AccountEO.iPermission);
            objAccountEO_Temp = _AccountEO;
            BindData_CBL(_AccountEO.sPermission);
        }

        protected void BindData_DDL()
        {
            ddlsUsername.DataSource = (new ShareDAO.UserDAO()).SelectBy_bState(null);
            ddlsUsername.DataValueField = "PK_iAccountID";
            ddlsUsername.DataTextField = "sUsername";
            ddlsUsername.DataBind();

            ddliPermission.DataSource = GetListConstants.Account_iPermission_GLC();
            ddliPermission.DataValueField = "Key";
            ddliPermission.DataTextField = "Value";
            ddliPermission.DataBind();

            cblPK_iFunctionID.DataSource = (new FunctionDAO()).SelectBy_isAll(BIT.iTrue);
            cblPK_iFunctionID.DataValueField = "PK_iFunctionID";
            cblPK_iFunctionID.DataTextField = "sName";
            cblPK_iFunctionID.DataBind();
        }

        protected void BindData_CBL(String _sPermission)
        {
            try
            {
                if (!String.IsNullOrEmpty(_sPermission))
                {
                    _sPermission = Security.DecryptString(_sPermission, ddlsUsername.SelectedValue);
                    string[] key = _sPermission.Split(',');
                    foreach (ListItem item in cblPK_iFunctionID.Items)
                    {
                        if (key.Contains(item.Value))
                            item.Selected = true;
                        else
                            item.Selected = false;
                    }
                }
                else
                    foreach (ListItem item in cblPK_iFunctionID.Items)
                    {
                        item.Selected = false;
                    }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }


        }

        protected void ddlsUsername_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData(false);
        }

        protected void ddliPermission_SelectedIndexChanged(object sender, EventArgs e)
        {
            String sPermission = (new FunctionDAO()).SelectBy_sGroup(Convert.ToInt16(ddliPermission.SelectedValue));
            sPermission = Security.EncryptString(sPermission, ddlsUsername.SelectedValue);
            BindData_CBL(sPermission);
        }

        protected void chkSetDefault_CheckedChanged(object sender, EventArgs e)
        {
            ddlsUsername.Enabled = chkSetDefault.Checked;
            if (chkSetDefault.Checked == false)
            {
                String sPermission = (new FunctionDAO()).SelectBy_sGroup(Convert.ToInt16(ddliPermission.SelectedValue));
                sPermission = Security.EncryptString(sPermission, ddlsUsername.SelectedValue);
                BindData_CBL(sPermission);
            }
            else
            {
                AccountEO _AccountEO = (new ShareDAO.UserDAO()).SelectItem(Convert.ToInt32(ddlsUsername.SelectedValue), null);
                ddliPermission.SelectedValue = Convert.ToString(_AccountEO.iPermission);
                //BindData_CBL(_AccountEO.sPermission);
            }
        }
    }
}